//
//  CPHomeController.swift
//  CJPackDemo
//
//  Created by 陈警卫 on 2023-12-05.
//

import UIKit
import CJPack
import Alamofire

class CPHomeController: CJBaseMenuController {

    var scrollV: UIScrollView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
        setupData()
    }
    
    override func cj_skinAction(_ kit: Any) {
        super.cj_skinAction(kit)
    }
    
}

extension CPHomeController {
    
    func setupData() {
        
    }
    
    func setupView() {
        
        scrollV = UIScrollView(frame: CGRect(x: 0.0, y: 0.0, width: view.cj_width, height: view.cj_height))
        scrollV.contentInsetAdjustmentBehavior = .never
        view.addSubview(scrollV)
        
        let params = [["name": "SwiftUI",    "vc": "CPSwiftUIController"],
                      ["name": "HUD",        "vc": "CPHudController"],
                      ["name": "Charts",     "vc": "CPChartsController"],
                      ["name": "OCCharts",   "vc": "OCPChartsController"],
                      ["name": "Theme",      "vc": ""],
                      ["name": "Pop",        "vc": "CPPopupController"],
                      ["name": "MJRefresh",  "vc": "CPMJRefreshController"],
                      ["name": "SDWebImage", "vc": "CPSDWebImageController"],
                      ["name": "ViewHolder", "vc": "CPHolderController"],
                      ["name": "Http",       "vc": "CPHttpController"],]
        
        var btns: [UIButton] = []
        
        for (index, param) in params.enumerated() {
            
            var y = CJScreen.navBatBarHeight + 10
            if index > 0 {
                if let btn = btns.last {
                    y = btn.cj_bottom + 10
                }
            }
            
            let title = param["name"]
            
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x: 20, y: y, width: 200, height: 30)
            btn.cj_backroundColorkey = CPColor.vistaModel.key
            btn.setTitle(title, for: .normal)
            btn.setTitleColor(CPColor.foreModel.color, for: .normal)
            btn.cj_addAction { [weak self] btn in
                self?.btnAction(btn: btn, param: param)
            }
            scrollV.addSubview(btn)
            btns.append(btn)
        }
        
        if let btn = btns.last {
            let bottom = btn.cj_bottom + 10
            scrollV.contentSize = CGSize(width: view.cj_width, height: bottom)
        }
        
        scrollV.cj_didSkin = { kit in
            for btn in btns {
                btn.setTitleColor(CPColor.foreModel.color, for: .normal)
            }
        }
    }
}

extension CPHomeController {
    
    func btnAction(btn: UIButton, param: [String: Any]) {
        
        guard let title = param["name"] as? String else { return }
        
        if title == "Theme" {
            themeBAction(btn: btn)
        } else {
            guard let vcString = param["vc"] as? String else { return }
            let isOC = title.hasPrefix("OC")
            guard let vcClass = isOC ? vcString.cj_class_oc() : vcString.cj_class() else { return }
            guard let vcType = vcClass as? UIViewController.Type else { return }
            let vc = vcType.init()
            navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    func themeBAction(btn: UIButton) {
        /**
         if CJSkin.shared.cj_currentSkin == "default" {
         CJSkin.shared.cj_set(currentSkin: "dark")
         } else {
         CJSkin.shared.cj_set(currentSkin: "default")
         }
         
         */
    }
}
